package com.panshi.modules.system.controller;


import com.panshi.api.util.CommonResult;
import com.panshi.api.util.R;
import com.panshi.modules.system.bean.BeiSysFunction;
import com.panshi.modules.system.bean.BeiSysRole;
import com.panshi.modules.system.service.BeiSysFunctionService;
import com.panshi.modules.system.service.BeiSysRoleService;
import com.panshi.modules.system.service.SysRoleFunctionService;
import com.panshi.modules.system.util.MyTree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.util.*;
import java.util.stream.Collectors;

/*
 * <p>
 * 功能表  前端控制器
 * </p>
 *
 * @author 小伍
 * @since 2020-08-07
 '*/

@Controller
@Slf4j
@Api(tags = "用户功能接口")
public class BeiSysFunctionController {

    @Autowired
    private BeiSysFunctionService beiSysFunctionService;

    @Autowired
    private BeiSysRoleService beiSysRoleService;

    @Autowired
    private SysRoleFunctionService sysRoleFunctionService;

    @GetMapping("/role/queryrid")
    @ApiOperation("根据rid与type查询管理")
   @RequiresPermissions("role:list")
    @ResponseBody
    public List<Object> queryFunByType(Integer rid) {
        boolean flag = false;
        List<Integer> integerList = new ArrayList<>();
        List<Object> list = new ArrayList<>();
        List<BeiSysRole> sysRoles = beiSysRoleService.querys();//查询所以角色
        for (BeiSysRole sysRole : sysRoles) {
            MyTree<List<Object>> myTree = new MyTree<>();
            myTree.setLabel(sysRole.getRname());
            myTree.setRid(sysRole.getRid());
            List<BeiSysFunction> b = beiSysFunctionService.querybyrid(sysRole.getRid());//查询当前角色的所以权限
            List<BeiSysFunction> c = new ArrayList<>();
            for (BeiSysFunction beiSysFunction : b) {
                if (beiSysFunction.getPid() == null) {
                    c.add(beiSysFunction);
                }
            }
            for (BeiSysFunction beiSysFunction : b) {
                if (beiSysFunction.getPid() != null) {
                    c.add(beiSysFunction);
                }
            }
            List<Object> lists = new ArrayList<>();
            for (BeiSysFunction bei : c) {
                if (bei.getPid() == null) {
                    integerList.add(bei.getFid());
                    MyTree<List<Object>> myTree2 = new MyTree<>();
                    myTree2.setRid(bei.getFid());
                    myTree2.setLabel(bei.getName());
                    lists.add(myTree2);
                    myTree.setChildren(lists);
                    List<BeiSysFunction> bs = beiSysFunctionService.queryFunction(bei.getFid(), sysRole.getRid());
                    List<Object> listtwo = new ArrayList<>();
                    for (BeiSysFunction beiSysFunction : bs) {
                        MyTree<List<Object>> myTree3 = new MyTree<>();
                        myTree3.setRid(beiSysFunction.getFid());
                        myTree3.setLabel(beiSysFunction.getName());
                        listtwo.add(myTree3);
                        myTree2.setChildren(listtwo);
                    }
                } else if (integerList.indexOf(bei.getPid()) == -1) {
                    MyTree<List<Object>> myTree2 = new MyTree<>();
                    myTree2.setRid(bei.getFid());
                    myTree2.setLabel(bei.getName());
                    lists.add(myTree2);
                    myTree.setChildren(lists);
                }
            }
            integerList = new ArrayList<>();
            list.add(myTree);
        }

        return list;
    }


    @GetMapping("/role/queryallpermissions")
    @ApiOperation("查询当前所以权限")
   @RequiresPermissions("role:list")
    @ResponseBody
    public List<Object> queryallpermissions() {
        List<Object> list = new ArrayList<>();
        List<BeiSysFunction> queryallpermissions = beiSysFunctionService.queryallpermissions();
        List<BeiSysFunction> collect = queryallpermissions.stream().distinct().collect(Collectors.toList());
        for (BeiSysFunction permission : collect) {
            MyTree<List<Object>> myTree = new MyTree<>();
            myTree.setLabel(permission.getName());
            myTree.setRid(permission.getFid());
            List<BeiSysFunction> queryper = beiSysFunctionService.queryper(permission.getFid());
            List<Object> lists = new ArrayList<>();
            for (BeiSysFunction beiSysFunction : queryper) {
                MyTree<List<Object>> myTree2 = new MyTree<>();
                myTree2.setRid(beiSysFunction.getFid());
                myTree2.setLabel(beiSysFunction.getName());
                lists.add(myTree2);
                myTree.setChildren(lists);
            }
            list.add(myTree);
        }
        return list;
    }

    @GetMapping("/role/querypermissionsbyrid")
    @ApiOperation("查询指定用户所以权限")
   @RequiresPermissions("role:list")
    @ResponseBody
    public  List<Integer> querybyrid(Integer rid){
       List<Integer> integerList=new ArrayList<>();
        List<BeiSysFunction> querybyrid = beiSysFunctionService.querybyrid(rid);
        querybyrid.forEach((s)->{
            integerList.add(s.getFid());
        });
        return integerList;
    }

    @GetMapping("/admin/admin-rule")
    @ApiOperation("返回权限页面")
    @RequiresPermissions("function:list")
    public String queryRoleRule(){
        return "/admin/admin-rule";
    }

    @GetMapping("/function/rule")
    @ApiOperation("查询功能")
   @RequiresPermissions("function:list")
    @ResponseBody
    public R queryBeiSysFun(Integer currentPage, Integer pageSize){
        Map<String,Integer> map=new HashMap<>();
        map.put("currentPage", currentPage);
        map.put("pageSize",pageSize);
        List<BeiSysFunction> beiSysFunctions = beiSysFunctionService.queryBeiSysFunction(map);
        List<BeiSysFunction> bei=beiSysFunctions;
        List<BeiSysFunction> list=new ArrayList<>();
        for (int i = 0; i < bei.size(); i++) {
            System.out.println(bei.get(i));
            BeiSysFunction common = common(bei.get(i));
            list.add(common);
        }
        Integer total = beiSysFunctionService.queryFunctioncount();
        return  R.ok().data("data",list).data("total",total);
    }

    //递归查询子节点
    public  BeiSysFunction common(BeiSysFunction beiSysFunction){
        if(beiSysFunction==null){
            return null;
        }
        List<BeiSysFunction> beiSysFunctions = beiSysFunctionService.queryFunctionByFid(beiSysFunction.getFid());
        beiSysFunction.setChildren(beiSysFunctions);
        for (int i = 0; i < beiSysFunctions.size(); i++) {
            common(beiSysFunctions.get(i));
        }
        return beiSysFunction;
    }

    @PostMapping("/function/update")
    @ApiOperation("查询功能")
    @RequiresPermissions("function:update")
    @ResponseBody
    public CommonResult updatestate(BeiSysFunction beiSysFunction){
        beiSysFunction.setUpdateTime(new Date());
        beiSysFunctionService.updateFunctionstate(beiSysFunction);
        return new CommonResult();
    }

    @PostMapping("/function/add")
    @ApiOperation("新增菜单")
   @RequiresPermissions("function:add")
    @ResponseBody
    public CommonResult addfunction(BeiSysFunction beiSysFunction){
        beiSysFunction.setCreateTime(new Date());
        beiSysFunction.setUpdateTime(new Date());
        beiSysFunction.setState(0);
        beiSysFunctionService.addfunction(beiSysFunction);
        return new CommonResult();
    }


    @PostMapping("/function/updatefunction")
    @ApiOperation("修改菜单") @RequiresPermissions("function:update")
    @ResponseBody
    public CommonResult updatefunction(BeiSysFunction beiSysFunction){
        beiSysFunction.setUpdateTime(new Date());
        beiSysFunctionService.updateFunction(beiSysFunction);
        return new CommonResult();
    }

    @GetMapping("/function/delete")
    @ApiOperation("删除菜单")
    @RequiresPermissions("function:delete")
    @ResponseBody
    public CommonResult deletefunction(Integer fid){
        beiSysFunctionService.deleteFunction(fid);
        sysRoleFunctionService.deleteRoleFunbyfid(fid);
        beiSysFunctionService.deleteFunctionbypid(fid);
        return new CommonResult();
    }

}


